package com.wzh.product.service.impl;

import com.wzh.product.dao.DiscountDao;
import com.wzh.product.dao.MydisDao;
import com.wzh.product.entity.DiscountEntity;
import com.wzh.product.entity.MyLog;
import com.wzh.product.entity.MydisEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.Query;

import com.wzh.product.dao.ProductDao;
import com.wzh.product.entity.ProductEntity;
import com.wzh.product.service.ProductService;


@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductDao, ProductEntity> implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Autowired
    private MydisDao mydisDao;

    @Autowired
    private DiscountDao discountDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductEntity> page = this.page(
                new Query<ProductEntity>().getPage(params),
                new QueryWrapper<ProductEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void addLog(MyLog myLog) {
        //添加商品
        Integer i=productDao.addLog(myLog);
        if (i>0){
            //修改我的优惠券状态
            MydisEntity mydisEntity = mydisDao.selectById(myLog.getDid());
            mydisEntity.setState("已使用");
            mydisDao.updateById(mydisEntity);
            //修改优惠券的可使用数量
            DiscountEntity discountEntity = discountDao.selectById(mydisEntity.getDisId());
            discountEntity.setKeYong(discountEntity.getKeYong()+1);
            discountDao.updateById(discountEntity);
            //修改商品个数
            ProductEntity productEntity = baseMapper.selectById(myLog.getPid());
            productEntity.setTotal(productEntity.getTotal()-1);
            baseMapper.updateById(productEntity);
        }
    }

}